package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dao.MerchantsDao;
import com.woniuxy.entity.*;
import com.woniuxy.service.MerchantsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @author Leimin
 * @date 2022/04/26 19:20
 **/
@Service
public class MerchantsServiceImpl implements MerchantsService {
    @Resource
    MerchantsDao merchantsDao;

    @Override
    public ResponseEntity findAllByCondition(QueryEntity<Merchants> queryEntity) {
        PageHelper.startPage(queryEntity.getCurrentPage(),queryEntity.getPageSize());

        String orderSort = queryEntity.getOrderSort();
        if("asc".equalsIgnoreCase(orderSort) || "desc".equalsIgnoreCase(orderSort)){
            PageHelper.orderBy(queryEntity.getOrderKey()+" "+orderSort);
        }

        List<Merchants> list = merchantsDao.findAllByCondition(queryEntity.getEntity());
        PageInfo<Merchants> pageInfo = PageInfo.of(list);
        return new ResponseEntity<>(200,"成功",pageInfo);
    }

    @Override
    public ResponseEntity findAll() {
        List<Merchants> list = merchantsDao.findAll();
        return new ResponseEntity<>(200,"成功",list);
    }

    @Override
    public ResponseEntity findOne(int id) {
        Merchants merchants = merchantsDao.findOne(id);
        return new ResponseEntity(200,"成功",merchants);
    }

    @Override
    public ResponseEntity addOne(Merchants merchants) {
        int index = merchantsDao.addOne(merchants);
        if (index>0){
            return ResponseEntity.SUCCESS;
        }
        return ResponseEntity.Fail;
    }

    @Override
    public ResponseEntity deleteOne(int id) {
        int index = merchantsDao.deleteOne(id);
        if (index>0){
            return ResponseEntity.SUCCESS;
        }
        return ResponseEntity.Fail;
    }

    @Override
    public ResponseEntity deleteBatch(int[] ids) {
        int index = merchantsDao.deleteBatch(ids);
        if (index>0){
            return ResponseEntity.SUCCESS;
        }
        return ResponseEntity.Fail;
    }

    @Override
    public ResponseEntity updateOne(Merchants merchants) {
        int index = merchantsDao.updateOne(merchants);
        if (index>0){
            return ResponseEntity.SUCCESS;
        }
        return ResponseEntity.Fail;
    }

    @Override
    public ResponseEntity findFinance(QueryEntity<Merchants> queryEntity) {
        PageHelper.startPage(queryEntity.getCurrentPage(),queryEntity.getPageSize());

        String orderSort = queryEntity.getOrderSort();
        if("asc".equalsIgnoreCase(orderSort) || "desc".equalsIgnoreCase(orderSort)){
            PageHelper.orderBy(queryEntity.getOrderKey()+" "+orderSort);
        }

        List<Merchants_finance> list = merchantsDao.findFinance(queryEntity.getEntity());
        PageInfo<Merchants_finance> pageInfo = PageInfo.of(list);
        return new ResponseEntity<>(200,"成功",pageInfo);
    }

    @Override
    public ResponseEntity findSelectOption() {
        List<SelectOption> typeOption = merchantsDao.findTypeOption();
        List<SelectOption> gradeOption = merchantsDao.findGradeOption();
        List<SelectOption> attributeOption = merchantsDao.findAttributeOption();
        List<SelectOption> industryOption = merchantsDao.findIndustryOption();
        HashMap<String, List> map = new HashMap<>();
        map.put("typeOption",typeOption);
        map.put("gradeOption",gradeOption);
        map.put("attributeOption",attributeOption);
        map.put("industryOption",industryOption);
        return new ResponseEntity(200,"成功",map);
    }

    @Override
    public ResponseEntity findByCode(String code) {
        Merchants merchants = merchantsDao.findByCode(code);
        if (merchants!=null){
            return new ResponseEntity(200,"成功",merchants);
        }
        return ResponseEntity.Fail;
    }

    @Override
    public ResponseEntity findByName(String name) {
        Merchants merchants = merchantsDao.findByName(name);
        if (merchants!=null){
            return new ResponseEntity(200,"成功",merchants);
        }
        return ResponseEntity.Fail;
    }

    @Override
    public ResponseEntity importExcel(List<Merchants> list) {
        int index = merchantsDao.addBatch(list);
        if (index>0){
            return ResponseEntity.SUCCESS;
        }
        return ResponseEntity.Fail;
    }
}

